﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace MediaPlayerControlLibrary
{
    /// <summary>
    /// Interaction logic for VolumeControl.xaml
    /// </summary>
    public partial class VolumeControl : UserControl
    {
        public VolumeControl()
        {
            InitializeComponent();
        }

        public double Value
        {
            get { return (double)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        private const double MinValue = 0, MaxValue = 100;

        /// <summary>
        /// Identifies the Value dependency property.
        /// </summary>
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register(
                "Value", typeof(double), typeof(VolumeControl),
                new FrameworkPropertyMetadata(MinValue, new PropertyChangedCallback(OnValueChanged)));

        private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            var control = (VolumeControl)obj;
            control.SetSliderValue();

            var e = new RoutedPropertyChangedEventArgs<double>(
                (double)args.OldValue, (double)args.NewValue, ValueChangedEvent);
            control.OnValueChanged(e);
        }

        private void SetSliderValue()
        {
            SetFilledPolygon(Value / 100 * BorderPolygon.RenderSize.Width);
        }

        /// <summary>
        /// Identifies the ValueChanged routed event.
        /// </summary>
        public static readonly RoutedEvent ValueChangedEvent = EventManager.RegisterRoutedEvent(
            "ValueChanged", RoutingStrategy.Bubble,
            typeof(RoutedPropertyChangedEventHandler<double>), typeof(VolumeControl));

        /// <summary>
        /// Occurs when the Value property changes.
        /// </summary>
        public event RoutedPropertyChangedEventHandler<double> ValueChanged
        {
            add { AddHandler(ValueChangedEvent, value); }
            remove { RemoveHandler(ValueChangedEvent, value); }
        }

        /// <summary>
        /// Raises the ValueChanged event.
        /// </summary>
        /// <param name="args">Arguments associated with the ValueChanged event.</param>
        protected virtual void OnValueChanged(RoutedPropertyChangedEventArgs<double> args)
        {
            RaiseEvent(args);
        }


        private void Polygon_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                SetFilledPolygon(e.GetPosition((Polygon)sender).X);
            }
        }

        private void SetFilledPolygon(double value)
        {
            var pHeight = BorderPolygon.RenderSize.Height;
            var pWidth = BorderPolygon.RenderSize.Width;

            Value = value / pWidth * 100;

            var W = (pHeight * pWidth) / (pHeight - 2);
            var pointCollection = new PointCollection();
            pointCollection.Add(new Point(0, pHeight));
            pointCollection.Add(new Point(0, pHeight - 2));
            pointCollection.Add(new Point(value, (1 - value / W) * pHeight - 2));
            pointCollection.Add(new Point(value, pHeight));

            FillPolygon.Points = pointCollection;
        }

        private void Polygon_MouseDown(object sender, MouseButtonEventArgs e)
        {
            SetFilledPolygon(e.GetPosition((Polygon)sender).X);
        }
    }
}
